﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Mirror;

public class Phone_CameralContol : NetworkBehaviour
{
    private ScenceScript scenceScript;

    public GameObject floatingInfo;
    public TextMesh nameText;

    private Material playerMateriaClone;

    public GameObject cam;
    public GameObject joy;

    private CharacterController cc;
    public float moveSpeed = 5;
    private float horizontalWrapMode, verticalMove;
    private Vector3 dir;
    public Joystick joystick;

    [SyncVar(hook = nameof(OnPlayerNameChanged))]
    private string playerName;

    [SyncVar(hook = nameof(OnPlayerColorChanged))]
    private Color playerColor;

    private void OnPlayerNameChanged(string oldStr, string newStr)
    {
        nameText.text = newStr;
    }

    private void OnPlayerColorChanged(Color oldCol,Color newCol)
    {
        nameText.color = newCol;

        playerMateriaClone = new Material(GetComponent<Renderer>().material);
        playerMateriaClone.SetColor("_EmissionColor", newCol);

        GetComponent<Renderer>().material = playerMateriaClone;
    }

    [Command]
    private void CmdSetupPlayer(string nameValue,Color colorValue)
    {
        playerName = nameValue;
        playerColor = colorValue;


        scenceScript.statusText = $"{playerName} joined.";
    }

    [Command]
    public void CmdSendPlayerMessage()//主动的消息发送
    {
        if(scenceScript)
        {
            scenceScript.statusText = $"{playerName} says hello{Random.Range(1, 99)}";
        }
    }
    public override void OnStartLocalPlayer()
    {
        //消息的赋值
        scenceScript.phone_CameralContol = this;

        cam.SetActive(true);
        joy.SetActive(true);
        //Camera.main.transform.SetParent(transform);
        //Camera.main.transform.localPosition = Vector3.zero;

        //让玩家的名称在自己视角的正下方
        floatingInfo.transform.localPosition = new Vector3(0, -.3f, .1f);
        floatingInfo.transform.localScale = new Vector3(.6f, .6f, .6f);

        
        //生成Player的随机名称
        var tempName = $"Player{Random.Range(500, 999)}";
        var tempColor = new Color
        (
            Random.Range(0f, 1f),
            Random.Range(0f, 1f),
            Random.Range(0f, 1f),
            1
        ) ;

        //将随机值传入cmdSetupPlayer
        CmdSetupPlayer(tempName, tempColor);
        

    }

    private void Awake()
    {
        cc = GetComponent<CharacterController>();

        scenceScript = FindObjectOfType<ScenceScript>();//获取ScenceScript
    }

    // Update is called once per frame
    void Update()
    {
        if (NetState.isNet == true)//联网模式角色移控制
        {
            //print("联网模式");
            if (!isLocalPlayer)
            {
                floatingInfo.transform.LookAt(Camera.main.transform);
                return;
            }
            horizontalWrapMode = joystick.Horizontal * moveSpeed;
            //verticalMove = joystick.Vertical * moveSpeed;
            dir = transform.forward * verticalMove;

            transform.Rotate(0, horizontalWrapMode / 5, 0);


            cc.Move(dir * Time.deltaTime);

            //print("yaogan");


            if (Input.GetKeyDown(KeyCode.C))
            {
                /*
                 * //生成Player的随机名称
                var tempName = $"Player{Random.Range(1, 999)}";
                var tempColor = new Color
                (
                    Random.Range(0f, 1f),
                    Random.Range(0f, 1f),
                    Random.Range(0f, 1f),
                    1
                );

                //将随机值传入cmdSetupPlayer
                CmdSetupPlayer(tempName, tempColor);
                */
            }

        }
    }
}
